<template>
  <section class="todoapp">
    <TodoHeader @add="addFn"></TodoHeader>
    <TodoMain :list="showList" @dal="dalFn"></TodoMain>
    <TodoFooter 
      :count="showList.length"
      :type="getSel"
      @changeType="changeFn"
      @clear="clearFn"
      ></TodoFooter>
  </section>
</template>

<script>
// 1.0 样式引入
import "./styles/base.css"
import "./styles/index.css"
    
import TodoHeader from "./components/TodoHeader";
import TodoMain from "./components/TodoMain";
import TodoFooter from "./components/TodoFooter";


export default {
  components: {
    TodoHeader,
    TodoMain,
    TodoFooter,
  },
  data() {
    return {
      list: JSON.parse(localStorage.getItem('todoList')) || [],
      getSel: 'all',
    }
  },
  methods: {
    addFn(taskname) {
      this.list.push({
        id: this.list.length ? this.list.at(-1).id + 1 : 100,
        name: taskname,
        isDone: false,
    })
    },
    dalFn(id) {
      this.list = this.list.filter(item => item.id !== id)
    },
    changeFn(type) {
      this.getSel = type
    },
    clearFn() {
      this.list = this.list.filter(item => !item.isDone)
    }
  },
  computed: {
     showList() {
      if (this.getSel === "yes") {
        return this.list.filter(item => item.isDone)
      } else if (this.getSel === "no") {
        return this.list.filter(item => !item.isDone)
      } else {
        return this.list
      }
     }
  },
  watch: {
    list: {
      deep: true,
      handler() {
        localStorage.setItem('todoList', JSON.stringify(this.list))
      }
    }
  }
};
</script>